package  
{
	import flash.display.Graphics;
	import flash.geom.Point;
	import samples.BaseSample;
	import systems.spidron.Spidron;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class SpidronSample_0 extends BaseSample 
	{
		private var iterations:int;
		private var size:Number;
		private var step:Number = Math.PI / 3;
		private var multiX:Number = Math.sin( step );
		private var multiY:Number = Math.cos( step );
		//private var colors:Vector.<uint> = Vector.<uint>( [ 0x354242, 0x7D9100, 0xC9DE55, 0xFFFF9D, 0xACEBAE ] );
		private var colors:Vector.<uint> = Vector.<uint>( [ 0x3B1E26, 0x701604, 0xAB3C0C, 0xD96B2F, 0xFF996E ] );
		private var colId:int = 0;
		public function SpidronSample_0() 
		{
			
			size = 100;
			iterations = 10;
			
			var w:int = sw / size + 1;
			var h:int = sh / size + 1;
			trace( w, h );
			var ox:Number, oy:Number;
			
			for ( var i:int = 0; i < w; i++ )
			{
				for ( var j:int = 0; j < h; j++ )
				{
					ox = i * 2;
					if( j % 2 == 0 )ox += 1
					oy = j * 3;
					rosace( ox * ( size * multiX ), oy * ( size * multiY )  );
				}
			}
		}
		
		private function rosace( x:Number, y:Number, dir:int = 1 ):void 
		{
			
			
			var i:Number;
			
			for ( i = 0; i < Math.PI * 2; i += step )
			{
				
				
				var sp:Spidron = new Spidron( x, y, size, 0, 35.35353535353535 / 180 * Math.PI )// / Math.sqrt( 3 ) );
				
				sp.reset();

				sp.x = x + Math.cos( i ) * ( size * multiX );
				sp.y = y + Math.sin( i ) * ( size * multiX );
				sp.angle = i + Math.PI / 2;
				sp.build( graphics, iterations );
				draw( sp );
				
				
			}
			
		}
		
		private function draw( spidron:Spidron ):void
		{
			var i:int;
			var a:Point, b:Point, c:Point, d:Point;
			
			for ( i = 0; i < spidron.points.length; i+=4 )
			{
				a = spidron.points[ i ];
				b = spidron.points[ i + 1 ];
				c = spidron.points[ i + 2 ];
				d = spidron.points[ i + 3 ];
				
				graphics.lineStyle( 0, 0, 0 ); 
				graphics.beginFill( colors[ colId++ % colors.length ] );
				
				drawTri( a, b, c, graphics );
				drawTri( a, c, d, graphics );
				graphics.endFill()
				
				graphics.lineStyle( 0, 0xDDDDDD ); 
				drawQuad( a, b, c, d, graphics );
				
			}
		}
		
		private function drawTri(a:Point, b:Point, c:Point, graphics:Graphics):void 
		{
			graphics.moveTo( a.x, a.y );
			graphics.lineTo( b.x, b.y );
			graphics.lineTo( c.x, c.y );
			graphics.lineTo( a.x, a.y );
		}
		
		private function drawQuad( a:Point, b:Point, c:Point, d:Point, graphics:Graphics):void 
		{
			graphics.moveTo( b.x, b.y );
			graphics.lineTo( c.x, c.y );
			graphics.moveTo( a.x, a.y );
			graphics.lineTo( d.x, d.y );
			
		}
	}

}